//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class WorkRepository : BaseRepository, IWorkRepository {
        public WorkRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Work work) {
            this.GetDbSet<Work>().Add(work);
            this.UnitOfWork.SaveChanges();
        }

        public Work GetWork(Guid workId) {
            return this.GetDbSet<Work>()
                .Where(c => c.Id == workId)
                .Single();
        }

        public void Update(Work updatedWork) {
            this.GetDbSet<Work>().Attach(updatedWork);
            this.SetEntityState(updatedWork, updatedWork.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid workId) {
            Work WorkToDelete = this.GetWork(workId);
            this.GetDbSet<Work>().Remove(WorkToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Work> Find()
        {
            return this.GetDbSet<Work>();
        }

        public IQueryable<Work> GetAll()
        {
            return this.GetDbSet<Work>();
        }
    }
}
